#include "../include/head.h"
#include "../lib/error.h"

#define BUFFERSIZE  4096
#define COPYMODE    0644

int main(int agc,char *agv[])
{
    int in_fd,out_fd,n_chars;
    char buf[BUFFERSIZE];

    if (agc != 3) {
        fprintf(stderr,"usage: %s source destination\n",*agv);
        exit(1);
    }

    if ((in_fd=open(agv[1],O_RDONLY))==-1) {
        oops("Cannot open ",agv[1]);
    }

    if ((out_fd=creat(agv[2],COPYMODE))==-1) {
        oops("Cannot creat ",agv[2]);
    }

    while ((n_chars=read(in_fd,buf,BUFFERSIZE))>0) {
        if (write(out_fd,buf,n_chars)!=n_chars) {
            oops("Write error to ",agv[2]);
        }
    }

    if (close(in_fd) == -1 || close(out_fd)==-1) {
        oops("Error closing files","");
    }

    return 0;
}
